import enquireJs from 'enquire.js'
import JSEncrypt from 'jsencrypt';

export function isDef (v){
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove (arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

export function isRegExp (v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function () {
      call && call(true)
    },
    unmatch: function () {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

export function getRsaCode(str) {
  let pubKey = `-----BEGIN PUBLIC KEY-----
  MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHfiUPwIHixt6UbN4eG9SHebM9
  yld5XatKKHjWRtk0Y5xeEJP/u6n0Z0mjg+newQpsxjpuJvSigAjJrQltY224vEac
  7zhN+0Y3O0ZcKKbm+37w09GVGtFVFB+FlY0U6YlJ8YDreNz0Rgd9s2K/EkqoWtYQ
  f7cWH2WJ3T3i9u75awIDAQAB
      -----END PUBLIC KEY-----`;
  let encryptStr = new JSEncrypt();
  encryptStr.setPublicKey(pubKey);
  let  data = encryptStr.encrypt(str.toString());
  return data;
}

const _toString = Object.prototype.toString
